package edu.android.com;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

public class edge extends View {

	public node firstNode,secondNode;
	public int mColor;
	
	public static boolean mWithCost = false;
	public static boolean mOrientated = false;
	public static boolean mWithFlow = false;
	
	private String text;
	private int mCost;
	private int mFlow,mCapacity;
	
	//Used in onDraw
	private Matrix mMatrix = new Matrix();
	private Path mPath;
	private Paint mPaint,mTextPaint,mPaintArrow;
	private Rect bounds = new Rect();
	
	private float dest[] = new float[6];
	
	public edge(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	public void initialize(){
		mColor = Color.WHITE;
		
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setColor(mColor);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(3 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f));
		
		mPaintArrow = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaintArrow.setColor(mColor);
		mPaintArrow.setStyle(Paint.Style.STROKE);
		mPaintArrow.setStrokeWidth(2 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f));
		
		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mTextPaint.setColor(Color.CYAN);
		mTextPaint.setTextSize(20 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f));
		
		text = "0";
		mCost = 0;
		mFlow = 0;
		mCapacity = 0;
		
		invalidate();
	}

	private float phase = 0;
	@Override
	public void onDraw(Canvas canvas){
		mPath = new Path();
		mPath.moveTo(firstNode.getX(), firstNode.getY());
		mPath.lineTo(secondNode.getX(), secondNode.getY());
		
		if(mFlow == 0){
			mPaint.setColor(mColor);
			mPaintArrow.setColor(mColor);
			
			mPaint.setPathEffect(null);
			canvas.drawPath(mPath, mPaint);
		}
		else{
			if(mFlow > 0){
				phase -= 0.5;
				if(phase < -1000)
					phase = 0;
			}
					
			PathEffect pe = new DashPathEffect(new float[]{10,5,5,5},phase);
			mPaint.setPathEffect(pe);
			canvas.drawPath(mPath, mPaint);
		}
		
		if(mWithCost || mWithFlow){
			mTextPaint.getTextBounds(text,0,text.length(),bounds);
			canvas.drawTextOnPath(text, mPath, (float)(getDistBetween(firstNode,secondNode) - bounds.width())/2, -2, mTextPaint);
			
		}
		
		if(mOrientated == true && mFlow == 0){//Orientated
			//Make it orientated
			mPath = new Path();
			
			double angle = Math.atan2( -(double)secondNode.getY() + firstNode.getY(),(double)secondNode.getX() - firstNode.getX());
			float x = (float)(secondNode.getX() - secondNode.getRay()*Math.cos(angle));
			float y = (float)(secondNode.getY() + secondNode.getRay()*Math.sin(angle));
			
			float x1 = x - 7 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f),y1 = y - 5 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f);
			float x2 = x - 7 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f),y2 = y + 5 * ((GraphTheory.heightScale + GraphTheory.widthScale)/2f);
			
			float src[] = {x,y,x1,y1,x2,y2};
			
			mMatrix.mapPoints(dest, src);
			mMatrix.setRotate((float)Math.toDegrees(-angle),x,y);
			
			mPath.moveTo(dest[2], dest[3]);
			mPath.lineTo(dest[0], dest[1]);
			mPath.lineTo(dest[4], dest[5]);
			
			canvas.drawPath(mPath, mPaintArrow);
		}
		
		//invalidate();
	}
	
	private double getDistBetween(node a,node b){
		return Math.sqrt((double)(a.getX() - b.getX()) * (a.getX() - b.getX()) + (a.getY() - b.getY()) * (a.getY() - b.getY()));
	}
	
	public void createEdge(node a,node b){
		firstNode = a;
		secondNode = b;
		a.addEdge(this);
		if(edge.mOrientated == false)
			b.addEdge(this);
	}
	
	public int getCost(){
		return mCost;
	}
	
	public void setCost(int c){
		mCost = c;
		text = c + "";
	}
	public void setColor(int color){
		mColor = color;
	}
	public void setFlow(int flow){
		mFlow = flow;
		text = flow + "/" + mCapacity;
	}
	public void setCapacity(int cap){
		mCapacity = cap;
		text = "0/" + cap;
	}
	public int getCapacity(){
		return mCapacity;
	}
}
